#!/usr/bin/env python
import shutil
import os
import sys

from distutils.core import setup, Command

ispy3 = False

try:
    from distutils.command.build_py import build_py_2to3 as build_py
    from distutils.command.build_scripts import build_scripts_2to3 as build_scripts
    ispy3 = True
except ImportError:
    # 2.x
    from distutils.command.build_py import build_py
    from distutils.command.build_scripts import build_scripts


class CleanCommand(Command):
    """ Class for cleaning previous builds """
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        build_dir = os.path.join(os.path.dirname(__file__), 'build')
        if os.path.isdir(build_dir):
            shutil.rmtree(build_dir)


class Test(Command):
    """ Class for Running Unit Tests """
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        if ispy3:
            # Run build script to run 2to3 for python3
            self.run_command('clean')
            self.run_command('build_py')
            build_dir = self.get_finalized_command('build_py').build_lib

            # Modify path to prevent using current directory
            sys.path.remove(os.getcwd())
            sys.path.insert(0, build_dir)

        # Run unit tests
        from dicom.test import run_tests
        run_tests.run()

setup(
    name="pydicom",
    packages=['dicom',
              'dicom.contrib',
              'dicom.examples',
              'dicom.test',
              'dicom.util',
              'dicom.test.performance'],
    version="0.9.9",
    cmdclass={'test': Test,
              'build_py': build_py,
              'build_scripts': build_scripts,
              'clean': CleanCommand},
    package_data={'dicom': ['testfiles/*.*', 'testcharsetfiles/*.dcm']},
    description="Pure python package for DICOM medical file reading and writing",
    author="Darcy Mason and contributors",
    author_email="darcymason@gmail.com",
    url="http://pydicom.googlecode.com",
    license="MIT license",
    keywords="dicom python medical imaging",
    classifiers=["License :: OSI Approved :: MIT License",
                 "Intended Audience :: Developers",
                 "Intended Audience :: Healthcare Industry",
                 "Intended Audience :: Science/Research",
                 "Development Status :: 5 - Production/Stable",
                 "Programming Language :: Python",
                 "Programming Language :: Python :: 2.6",
                 "Programming Language :: Python :: 2.7",
                 "Programming Language :: Python :: 3.0",
                 "Programming Language :: Python :: 3.1",
                 "Programming Language :: Python :: 3.2",
                 "Programming Language :: Python :: 3.3",
                 "Programming Language :: Python :: 3.4",
                 "Operating System :: OS Independent",
                 "Topic :: Scientific/Engineering :: Medical Science Apps.",
                 "Topic :: Scientific/Engineering :: Physics",
                 "Topic :: Software Development :: Libraries"],
    long_description="""
    pydicom is a pure python package for parsing DICOM files.
    DICOM is a standard (http://medical.nema.org) for communicating
    medical images and related information such as reports
    and radiotherapy objects.

    pydicom makes it easy to read these complex files into natural
    pythonic structures for easy manipulation.
    Modified datasets can be written again to DICOM format files.
    See the `Getting Started <http://code.google.com/p/pydicom/wiki/GettingStarted>`_
    wiki page for installation and basic information, and the
    `Pydicom User Guide <http://code.google.com/p/pydicom/wiki/PydicomUserGuide>`_ page
    for an overview of how to use the pydicom library.
    """
)
